import { Injectable } from '@nestjs/common';
import { CreateInterviewDto } from './dto/create-interview.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Interview } from './entities/interview.entity';
import { Repository } from 'typeorm';
import { CategoryDto } from './dto/category.dto';
import { Category } from './entities/category.entity';

@Injectable()
export class InterviewService {
  constructor(
    @InjectRepository(Interview)
    private interviewRepository: Repository<Interview>,
    @InjectRepository(Category)
    private categoryRepository: Repository<Category>,
  ) {}

  async postDelArticle(interviewDto: CreateInterviewDto) {
    const queryBuilder = this.interviewRepository.createQueryBuilder();
    const { uid } = interviewDto;
    const res = await queryBuilder
      .delete()
      .from(Interview)
      .where('uid = :uid', { uid })
      .execute();
    return res;
  }

  async postQueryArticle(interviewDto: CreateInterviewDto) {
    const queryBuilder = this.interviewRepository.createQueryBuilder('article');
    let res = null;
    const { title, author } = interviewDto;
    queryBuilder.select([
      'article.uid',
      'article.title',
      'article.content',
      'article.author',
      'category.name',
    ]);
    // queryBuilder.leftJoin('article.category', 'category');
    queryBuilder.leftJoinAndMapOne(
      'article.category',
      Category,
      'category',
      'category.name = article.categoryName',
    );
    if (title) {
      queryBuilder.where('title = :title', { title });
    }
    if (author) {
      queryBuilder.where('author = :author', { author });
    }
    res = await queryBuilder.getMany();
    return res;
  }

  // 创建分类
  async postCreateCategory(categoryDto: CategoryDto) {
    const { name } = categoryDto;
    const queryBuilder = this.categoryRepository.createQueryBuilder();
    const categoryList = await this.postQueryCategory(categoryDto);
    if (categoryList.length) {
      return {
        msg: 'category is exist',
      };
    }
    const res = await queryBuilder
      .insert()
      .into(Category)
      .values({ name })
      .execute();

    return res;
  }

  // 查询分类
  async postQueryCategory(categoryDto: CategoryDto) {
    const { name } = categoryDto;
    const queryBuilder = this.categoryRepository.createQueryBuilder();
    let res = null;
    if (name) {
      queryBuilder.where('name = :name', { name });
    }
    res = await queryBuilder.getMany();
    return res;
  }

  // 保存文章
  async postSaveArticle(body: CreateInterviewDto) {
    const { uid } = body;
    const article = await this.findOne(uid);
    if (article) {
      this.update(body);
    } else {
      this.create(body);
    }
  }

  findAll() {
    return 'find all';
  }

  // 创建文章
  async create(body) {
    const res = await this.interviewRepository
      .createQueryBuilder()
      .insert()
      .into(Interview)
      .values(body)
      .execute();
    return res;
  }

  async findOne(uid: string) {
    const queryBuilder = this.interviewRepository.createQueryBuilder();
    const res = await queryBuilder.where('uid = :uid', { uid }).getOne();
    return res;
  }

  // 更新文章
  async update(body: CreateInterviewDto) {
    const queryBuilder = this.interviewRepository.createQueryBuilder();
    const { uid, content, author, title, category } = body;
    const res = await queryBuilder
      .update(Interview)
      .set({ content, author, title, category })
      .where('uid = :uid', { uid })
      .execute();

    return res;
  }

  remove(id: number) {
    return `This action removes a #${id} interview`;
  }
}
